﻿$(function () {
    $('#gvManage').datagrid({
        height: 480,
        method: 'POST',
        idField: 'UserID',
        striped: true,
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        showFooter: true,
        loadMsg: null,
        columns: [[
                    { field: 'UserID', title: '编号', width: 50, align: 'left' },
                    { field: 'UserName', title: '登陆名', width: 200, align: 'left' },
                    { field: 'State', title: '状态', width: 50, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            return value == 1 ? '<font color="Green">√</font>' : '<font color="red">×</font>';
                        }
                    }
                ]],
        loader: function (param, success, error) {
            QuickLifeHall.Facade.SystemBase.User.GetPageData(JSON.stringify(param), function (result) {
                if (result && result.error == null) {
                    success(result.value);
                }
                else {
                    alert(result.error.Message);
                    error();
                }
            });
        },
        onSelect: function (rowIndex, rowData) {
            //设置按钮的可用性
            if (rowData.State == 1) {
                $("#lbtnDisable").linkbutton("enable");
                $("#lbtnEnable").linkbutton("disable");
            }
            else {
                $("#lbtnDisable").linkbutton("disable");
                $("#lbtnEnable").linkbutton("enable");
            }
        },
        onBeforeLoad: function (param) {
            var txtUserID = $("#txtUserID").val();
            var txtUserName = $("#txtUserName").val();
            param.txtUserID = txtUserID;
            param.txtUserName = txtUserName;
        }
    });

    //在调整了窗口大小以后，设置easyui gridview也调整宽度
    $(window).resize(function () {
        $('#gvManage').datagrid('resize');
    });
})

function _add() {
    showWindow({
        title: '添加用户',
        url: '/pages/systembase/user/useradd.aspx',
        width: 500,
        height: 300,
        onClose: function () { _search(); }
    });
}

function _edit() {
    var selectedRow = $('#gvManage').datagrid('getSelected');
    if (selectedRow == null) {
        $.messager.alert("提示", "请选中一行数据");
        return;
    }

    showWindow({
        title: '修改密码',
        url: '/pages/systembase/user/UserEditPassword.aspx',
        param:selectedRow,
        width: 500,
        height: 300,
        onClose: function () { _search(); }
    });
}

function _delete() {
    var selectedRow = $('#gvManage').datagrid('getSelected');
    if (selectedRow == null) {
        $.messager.alert("提示", "请选中一行数据");
        return;
    }
    $.messager.confirm('提示', '确认要删除选中的记录吗?', function (result) {
        if (result) {
            QuickLifeHall.Facade.SystemBase.User.DeleteUser(selectedRow.UserID, function (result) {
                if (result && result.error == null) {
                    alert(result.value.message);
                    $('#gvManage').datagrid("load");
                }
            });
        }
    });
}

function _enable(enable) {
    var selectedRow = $('#gvManage').datagrid('getSelected');
    if (selectedRow == null) {
        $.messager.alert("提示", "请选中一行数据");
        return;
    }

    QuickLifeHall.Facade.SystemBase.User.EnableUser(selectedRow.UserID, enable, function (result) {
        if (result && result.error == null) {
            $('#gvManage').datagrid("load");
        }
    });
}

function _search() {
    $('#gvManage').datagrid("load");
}
